#!/usr/bin/env python3
# This file is part of Xpra.
# Copyright (C) 2016 Antoine Martin <antoine@xpra.org>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

import unittest
import binascii

from xpra.codecs.pillow.decoder import get_image_type


class TestPillowHeader(unittest.TestCase):

    def test_known_header(self):
        examples = {
            "png"   : "89504e470d0a1a0a0000000d4948445200000001000000010802000000907753de0000000c4944415408d763f8ffff3f0005fe02fedccc59e70000000049454e44ae42608227",
            "webp"  : "524946462600000057454250565038201a0000003001009d012a0100010000c01225a400037000fefedd78000000",
            "jpeg"  : "ffd8ffe000104a46494600010101012c012c0000fffe00134372656174656420776974682047494d50ffdb0043000302020302020303030304030304050805050404050a070706080c0a0c0c0b0a0b0b0d0e12100d0e110e0b0b1016101113141515150c0f171816141812141514ffdb00430103040405040509050509140d0b0d1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414ffc00011080001000103011100021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00fd53a00fffd9",
            }
        for encoding, hexdata in examples.items():
            data = binascii.unhexlify(hexdata)
            e = get_image_type(data)
            assert encoding==e, "expected %s got %s" % (encoding, e)

    def test_bad_header(self):
        for x in (b"", None,
                  b"abc",
                  b"\x99PNG",
                  b"RIF",
                  b"RIFF&\x00\x00\x00WEBp",
                  binascii.unhexlify("fe0000000000000000000000000000000000000000000000000000"),
                  ):
            e = get_image_type(x)
            if e:
                raise RuntimeError(f"wrongly detected {x} as {e}")


def main():
    unittest.main()

if __name__ == '__main__':
    main()
